package com.cy.ppj.web.admin.mall;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.mall.GoodsTypeAddAO;
import com.cy.ppj.model.ao.mall.GoodsTypeEditAO;
import com.cy.ppj.model.domian.mall.GoodsType;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.GoodsTypeAdminListQuery;
import com.cy.ppj.model.vo.mall.GoodsTypeAdminListVO;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.mall.GoodsTypeService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Description 后台-商品品类管理
 * @Author tanyilin
 * @Date 2019/9/27 14:58
 */
@RestController
@RequestMapping("/admin/mall/goods/type")
@Api(tags = "后台-商品品类管理")
public class AdminGoodsTypeController extends BaseController {

    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private FileService fileService;

    @ApiOperation("品类列表")
    @GetMapping("/list")
    public PageBean<GoodsTypeAdminListVO> getGoodsTypeAdminList(GoodsTypeAdminListQuery query) {

        PageBean<GoodsTypeAdminListVO> pageBean = goodsTypeService.getGoodsTypeAdminList(query);
        return pageBean;
    }

    @ApiOperation("新增商品品类")
    @PostMapping("/do/add")
    public Result doAddGoodsType(@Validated GoodsTypeAddAO addAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        GoodsType goodsType = new GoodsType();
        BeanUtils.copyProperties(addAO, goodsType);
        goodsTypeService.addGoodsType(goodsType);

        return ok("新增商品品类成功");
    }

    @ApiOperation("编辑商品品类")
    @PostMapping("/do/edit")
    public Result doEditGoodsType(@Validated GoodsTypeEditAO editAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        GoodsType goodsType = new GoodsType();
        BeanUtils.copyProperties(editAO, goodsType);
        goodsType.setId(editAO.getGoodsTypeId());
        goodsTypeService.updateGoodsType(goodsType);

        return ok("编辑商品品类成功");
    }

    @ApiOperation("上下架状态")
    @PostMapping("/update/status")
    public Result updateGoodsTypeStatus(@RequestParam(value = "goodsTypeId") Long goodsTypeId, @RequestParam(value = "status") CommonStatus status) {

        GoodsType goodsType = new GoodsType();
        goodsType.setId(goodsTypeId);
        goodsType.setStatus(status);
        goodsTypeService.updateGoodsType(goodsType);

        return ok("更改商品品类状态成功");
    }

    @ApiOperation("全部的商品品类列表，不分页")
    @GetMapping("/get/all/list")
    public Result<List<GoodsType>> getAllGoodsTypeList() {

        List<GoodsType> list = goodsTypeService.getAllGoodsTypeList();
        return ok("获取成功", list);
    }

    @ApiOperation("上传商品品类logo图片")
    @PostMapping("/upload/image")
    public Result uploadImage(@RequestParam(value = "file") MultipartFile file) {

        String directory = "goods/";
        Result<Map<String, String>> uploadResult = fileService.uploadToMallBucket(file, directory, null);
        return uploadResult;
    }
}
